package com.wallj.piececode;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.graphics.Palette;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by walljiang on 2016/3/20.
 */
public class PaletteDemo extends Activity {
    ViewPager pager;
    LinearLayout titleLL;
    TextView titleTV;
    ArrayList<ImageView> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_palette);
        pager = (ViewPager) findViewById(R.id.pager_palette);
        titleLL = (LinearLayout) findViewById(R.id.ll_title);
        titleTV = (TextView) findViewById(R.id.tv_title);
        titleTV.setText("白人美女");
        ImageView img1 = (ImageView) LayoutInflater.from(this).inflate(R.layout.item_imageview, null);
        img1.setImageResource(R.drawable.white_beauty);
        datas.add(img1);
        ImageView img2 = (ImageView) LayoutInflater.from(this).inflate(R.layout.item_imageview, null);
        img2.setImageResource(R.drawable.black_beauty);
        datas.add(img2);
        pager.setAdapter(new MyPagerAdapter());
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if (i == 0) {
                    titleTV.setText("白人美女");
                } else {
                    titleTV.setText("黑人美女");
                }
                Palette.generateAsync(((BitmapDrawable) datas.get(i).getDrawable()).getBitmap(),
                        new Palette.PaletteAsyncListener() {
                            @Override
                            public void onGenerated(Palette palette) {
                                Palette.Swatch vibrant =
                                        palette.getVibrantSwatch();
                                Palette.Swatch darkMute = palette.getDarkMutedSwatch();
                                if (vibrant != null) {
                                    // If we have a vibrant color
                                    // update the title TextView
                                    pager.setBackgroundColor(
                                            vibrant.getRgb());
                                } else {
                                    if (darkMute != null) {
                                        pager.setBackgroundColor(darkMute.getRgb());
                                    }
                                }
                            }
                        });
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    private class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(datas.get(position));
            return datas.get(position);
        }
    }

}
